# Read in the oil spill data
oil_spill <- read_sf(dsn = here("data"),
layer = "Oil_Spill_Incident_Tracking_%5Bds394%5D") %>%
st_transform(crs = 4326) %>% # Update CRS
clean_names()
# Read in California counties outline
ca_counties <- read_sf(dsn = here("data"),
layer = "california_county_shape_file") %>%
clean_names()
# Set CRS for CA counties layer
st_crs(ca_counties) <- 4326 # Set CRS to match
# Make an interactive map
tmap_mode("view") # set tmap viewing mode to interactive
tm_shape(ca_counties) +
tm_borders() +
tm_shape(oil_spill) +
tm_dots(col = "sienna2")
Figure 1. Interactive map displaying the locations of California oil spill events in 2008.
# Spatial join
ca_oil_spills <- ca_counties %>%
st_join(oil_spill)
# Find counts of inland oil spill events by county
spill_counts <- ca_oil_spills %>%
count(name)
# Plot a chloropleth map
ggplot(data = spill_counts) +
geom_sf(aes(fill = n), color = "white", size = 0.1) +
scale_fill_gradientn(colors = c("lightgray","orange","red")) +
theme_minimal() +
labs(fill = "Number of inland oil spills",
x = "Longitude",
y = "Latitude",
title = "California 2008 Inland Oil Spills")
Figure 2. Choropleth map depicting the number of inland oil spill events in 2008 by California county.
Citations: Oil spill data: CA DFW Oil Spill Incident Tracking Database System (dataset ds394), 2008.; California county shapefile: ESRI, ESM244